<?php
require_once 'resize/ThumbLib.inc.php';
//require_once 'general_pi.php';
require_once('../../assets/define.php');
//include('simpleimage.php'); 


$_GET['source']=str_replace(baseinfo,rootpath,$_GET['source']);



//}
if(strpos($_GET['source'],rootpath)!==false)
{

   $arraypic=explode("/",$_GET['source']);
   
   $namepic=$arraypic[count($arraypic)-1];
   list($namefile,$exitfile)=explode(".",$namepic);
   $newnamepic=$namefile.'-'.$_GET['width'].'x'.$_GET['height'].'.'.$exitfile;
   $newpath=str_replace($namepic,$newnamepic,$_GET['source']);
   if(!is_file($newpath))
   {
   $thumb = PhpThumbFactory::create($_GET['source']);
   $thumb->setOptions(array('bgcolor'=> array(255, 255, 255)));
   
   foreach($thumb->getCurrentDimensions() as $key => $value)
     $$key = $value;
     
     
        if(($_GET['width']&&$_GET['height']==0)||($_GET['width']==0&&$_GET['height']))
        {
        
        //exit;
        if($_GET['width']==0)
        {
        $widthnow= intval(($_GET['height']*$width)/($height));
        $heightnow=$_GET['height'];
        }else if($_GET['height']==0)
        {
        $widthnow=$_GET['width'];
        $heightnow=intval(($_GET['width']*$height)/($width));
        
        }
       // echo "width ".$widthnow."<br>";
//        echo "height ".$heightnow."<br>";
//        exit;
        
        $thumb->resize($widthnow, $heightnow,true);
        }
        else if ($width < $_GET['width']||$height<$_GET['height'])
        {
        $thumb->resize($_GET['width'], $_GET['height'],true);

        }
        else
        {
        $thumb->adaptiveResize($_GET['width'], $_GET['height']);
        }
     
     $thumb->save($newpath,'JPG');
     @chmod($newpath,0777);
   
   }
   
   $_GET['source']=str_replace(rootpath,baseinfo,$newpath);

    header("Location:".$_GET['source']); 

}
else if(strpos($_GET['source'],"img.youtube.com")!==false)
{

  $thumb = PhpThumbFactory::create($_GET['source']);
   $thumb->setOptions(array('bgcolor'=> array(255, 255, 255)));
   
   
   $arraypic=explode("/",$_GET['source']);
   $namefile=$arraypic[count($arraypic)-2];
   $newnamepic=$namefile.$_GET['width'].'x'.$_GET['height'].'.jpg';
   $newpath=rootpathinfo."/images/cache/".$newnamepic;
    if(!is_file($newpath))
   {
    foreach($thumb->getCurrentDimensions() as $key => $value)
     $$key = $value;
        if(($_GET['width']&&$_GET['height']==0)||($_GET['width']==0&&$_GET['height']))
        {

        $thumb->resize($_GET['width'], $_GET['height'],true);
        }
        else if ($width < $_GET['width']||$height<$_GET['height'])
        {
        $thumb->resize($_GET['width'], $_GET['height'],true);

        }
        else
        {
        $thumb->adaptiveResize($_GET['width'], $_GET['height']);
        }
  
     $thumb->save($newpath,'JPG');
   
    // $thumb->save($newpath,'JPG');
     @chmod($newpath,0777);
   }
   
   $_GET['source']=str_replace(rootpathinfo,baseinfo,$newpath);

    header("Location:".$_GET['source']); 

}
else if(strpos($_GET['source'],"vimeocdn.com")!==false)
{

  $thumb = PhpThumbFactory::create($_GET['source']);
   $thumb->setOptions(array('bgcolor'=> array(255, 255, 255)));
   
   
   $arraypic=explode("/",$_GET['source']);
   $namepic=$arraypic[count($arraypic)-1];
     list($namefile,$exitfile)=explode(".",$namepic);
   $newnamepic=$namefile.$_GET['width'].'x'.$_GET['height'].'.'.$exitfile;
   $newpath=rootpathinfo."/images/cache/".$newnamepic;
    if(!is_file($newpath))
   {
    foreach($thumb->getCurrentDimensions() as $key => $value)
     $$key = $value;
        if(($_GET['width']&&$_GET['height']==0)||($_GET['width']==0&&$_GET['height']))
        {

        $thumb->resize($_GET['width'], $_GET['height'],true);
        }
        else if ($width < $_GET['width']||$height<$_GET['height'])
        {
        $thumb->resize($_GET['width'], $_GET['height'],true);

        }
        else
        {
        $thumb->adaptiveResize($_GET['width'], $_GET['height']);
        }
  
     $thumb->save($newpath,'JPG');
   
  //   $thumb->save($newpath,'JPG');
     @chmod($newpath,0777);
   }
   
   $_GET['source']=str_replace(rootpathinfo,baseinfo,$newpath);

    header("Location:".$_GET['source']); 

}
else if(strpos($_GET['source'],"amazonaws")!==false)
{


 


   
   
   $arraypic=explode("/",$_GET['source']);
   $namepic=$arraypic[count($arraypic)-1];
     list($namefile,$exitfile)=explode(".",$namepic);
     
     
     
     
     
   $newnamepic=$namefile.$_GET['width'].'x'.$_GET['height'].'.'.$exitfile;
   $newpath=rootpathinfo."/images/cache/".$newnamepic;
   
   
   $newpathsource=rootpathinfo."/images/cache/".$namepic;

       //$img = file_get_contents($_GET['source']);
      // file_put_contents($newpathsource, $img);
     // echo $_GET['source'];
     if(!is_file($newpathsource))copy($_GET['source'],$newpathsource);
      
      
      
   

   
     
   
   
    if(!is_file($newpath))
   {
   $thumb = PhpThumbFactory::create($newpathsource);
    $thumb->setOptions(array('bgcolor'=> array(255, 255, 255)));
    foreach($thumb->getCurrentDimensions() as $key => $value)
     $$key = $value;
        if(($_GET['width']&&$_GET['height']==0)||($_GET['width']==0&&$_GET['height']))
        {

        $thumb->resize($_GET['width'], $_GET['height'],true);
        }
        else if ($width < $_GET['width']||$height<$_GET['height'])
        {
        $thumb->resize($_GET['width'], $_GET['height'],true);

        }
        else
        {
        $thumb->adaptiveResize($_GET['width'], $_GET['height']);
        }
  
     $thumb->save($newpath,'JPG');
   
     $thumb->save($newpath,'JPG');
     @chmod($newpath,0777);
   }
   
   $_GET['source']=str_replace(rootpathinfo,baseinfo,$newpath);

    header("Location:".$_GET['source']); 

}
else if($_GET['source']&&$_GET['newpath'])
{
   $thumb = PhpThumbFactory::create($_GET['source']);
   $thumb->setOptions(array('bgcolor'=> array(255, 255, 255)));
   $newpath=$_GET['newpath'];
   
      if(($_GET['width']&&$_GET['height']==0)||($_GET['width']==0&&$_GET['height']))
        {
        
        //exit;
        if($_GET['width']==0)
        {
        $widthnow= intval(($_GET['height']*$width)/($height));
        $heightnow=$_GET['height'];
        }else if($_GET['height']==0)
        {
        $widthnow=$_GET['width'];
        $heightnow=intval(($_GET['width']*$height)/($width));
        
        }

        
        $thumb->resize($widthnow, $heightnow,true);
        }
        else if ($width < $_GET['width']||$height<$_GET['height'])
        {
        $thumb->resize($_GET['width'], $_GET['height'],true);

        }
        else
        {
        $thumb->adaptiveResize($_GET['width'], $_GET['height']);
        }
     
     $thumb->save($newpath,'JPG');
     @chmod($newpath,0777);
     
     
   $_GET['source']=str_replace(rootpath,baseinfo,$newpath);

    header("Location:".$_GET['source']); 
   
   

}

exit();




?>
